from flask import Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask_mqtt import Mqtt
from flask_bootstrap import Bootstrap

from flask_serial import Serial
from config import config

# from flask_socketio import SocketIO
# import eventlet
# eventlet.monkey_patch()

app=Flask(__name__)
db = SQLAlchemy()

mqtt = Mqtt()
bootstrap = Bootstrap()
# socketio = SocketIO()
serial = Serial()

def create_app(config_name):
    # app.config.from_object('config.default')
    app.config.from_object(config[config_name])
    app.secret_key = '''\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!\xd5\xa2\xa0\x9fR"\xa1\xa8'''
    config[config_name].init_app(app)

    # print(app.config)
    db.init_app(app)
    mqtt.init_app(app)
    bootstrap.init_app(app)
    serial.init_app(app)
    # socketio.init_app(app)
    from .views.admin import myadmin
    myadmin.init_app(app)
    serial.ser.loop_start()


    from .views.index import idx
    app.register_blueprint(idx)

    return app

if __name__ == '__main__':
    app = create_app('development')
    app.run()